package com.timoon.manager.panel.passage.cell;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.upload.FileUpload;
import org.apache.wicket.markup.html.form.upload.FileUploadField;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.resource.PackageResourceReference;
import org.apache.wicket.spring.injection.annot.SpringBean;

import sun.print.resources.serviceui;

import com.googlecode.wicket.jquery.ui.form.button.AjaxButton;
import com.timoon.app.function.UpdateFunction;
import com.timoon.app.resource.image.ImageLocation;
import com.timoon.component.panel.editor.EditorPanel;
import com.timoon.manager.auth.BaseAuthPanel;
import com.timoon.manager.panel.passage.PassageManagerService;
import com.timoon.sample.dao.model.Passage;
import com.timoon.sample.dao.model.PassagePic;

/**
 *<p>功能:</p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013</p>
 *<p>Company:sky</p>
 *@author 付乐天
 *@version 1.0 */

public class PassageDetailPanel extends BaseAuthPanel {
	
	@SpringBean(name="manager.passage")
	private PassageManagerService service;

	private Passage passage;
	
	private FileUploadField fileUploadField;
	
	private List<PassagePic> passagePics=new ArrayList<>();
	
	private ModalWindow win;
	
	private UpdateFunction mainPanel;
	
	public PassageDetailPanel(String id,Passage passage,ModalWindow win,UpdateFunction mainPanel) {
		super(id);
		// TODO Auto-generated constructor stub
		
		this.passage=passage;
		
		this.win=win;
		
		this.mainPanel=mainPanel;
		
		renderPanel();
		
	}

	private void renderPanel() {
		// TODO Auto-generated method stub
		
		renderForm();
	}

	private void renderForm() {
		// TODO Auto-generated method stub
		
		Form form=new Form("wk_form");
		form.setOutputMarkupId(true);
		add(form);
		
		renderInput(form);
		
		
		renderPublicedSelect(form);
		
		renderSubmit(form);
		
		renderPassagePicList(form);
	}

	private void renderPassagePicList(final Form form) {
		// TODO Auto-generated method stub
		
		List<PassagePic> pics=this.service.getPassagePicsByPassageId(passage.getId());
		
		ListView<PassagePic> listView=new ListView<PassagePic>("wk_passage_pic_list",pics) {
			
			@Override
			protected void populateItem(ListItem<PassagePic> arg0) {
				// TODO Auto-generated method stub
				final PassagePic pic=arg0.getModelObject();
				
				arg0.add(new Image("wk_passage_pic", new PackageResourceReference(ImageLocation.class,pic.getPath())));
				arg0.add(new AjaxLink("wk_del_pic") {

					@Override
					public void onClick(AjaxRequestTarget arg0) {
						// TODO Auto-generated method stub
						service.delPassagePicById(pic.getId());
						arg0.appendJavaScript("$('#"+this.getMarkupId()+"').parent().empty();");

					}
				});
			}
		};
		form.add(listView);
	}

	private void renderSubmit(Form form) {
		// TODO Auto-generated method stub
		form.add(new AjaxButton("wk_submit") {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// TODO Auto-generated method stub
				super.onSubmit(target, form);
				
				
				FileUpload upload=fileUploadField.getFileUpload();
				
				if(upload!=null){

					StringBuffer picName=new StringBuffer(upload.getClientFileName());
					
					StringBuffer path=new StringBuffer(ImageLocation.class.getClassLoader().getResource("com/timoon/app/resource/image/passage/").toString());
					
					//删除path中开头的"file:\"
					String starting=path.substring(6);
					
					//截取客户端图片后缀
					String ending=picName.substring(picName.lastIndexOf("."));
					
					//获取系统时间
					String newPicName=String.valueOf((new Date()).getTime())+ending;//系统时间+后缀
					
					String newName=starting+newPicName;
					
					try {
						upload.writeTo(new File(newName));
						PassagePic pic=new PassagePic();
						pic.setPath("passage/"+newPicName);
						passagePics.add(pic);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				service.addOrUpadtePassage(passage, passagePics);
				
				win.close(target);
				mainPanel.doUpdate(target, null);
			}
			
		});
	}

	private void renderPublicedSelect(Form form) {
		// TODO Auto-generated method stub
		List<String> sites=new ArrayList<>();
		sites.add("1");
		sites.add("0");
		
		IChoiceRenderer<String> render=new ChoiceRenderer<String>(){
			@Override
			public Object getDisplayValue(String object) {
				// TODO Auto-generated method stub
				System.err.println(object);
				if(object.equals("0")){
					return "隐藏";
				}else{
					return "公开";
				}
			}
		};
		
		form.add(new DropDownChoice<String>("wk_detail_publiced", new PropertyModel<String>(passage, "publiced"),sites, render));
	}

	private void renderInput(Form form) {
		// TODO Auto-generated method stub
		
		form.add(new TextField("wk_detail_id",new PropertyModel<>(passage, "id")));
		form.add(new TextField("wk_detail_title",new PropertyModel<>(passage, "title")));
		form.add(new TextField("wk_detail_author",new PropertyModel<>(passage, "author")));
		
		form.add(new TextField("wk_detail_date",new PropertyModel<>(passage, "updateDate")){
			@Override
			public boolean isEnabled() {
				// TODO Auto-generated method stub
				return false;
			}
		});
		
		form.add(new TextField("wk_detail_red",new PropertyModel<>(passage, "readCount")){
			@Override
			public boolean isEnabled() {
				// TODO Auto-generated method stub
				return false;
			}
		});
		
		form.add(new EditorPanel("wk_content", new PropertyModel<>(passage, "content"), false));
		
		fileUploadField=new FileUploadField("wk_pic_upload");
		form.add(fileUploadField);
	}

}
